/**
 * @class   wosh::network::NetworkProtocolTcp
 * @brief   wosh::NetworkProtocol implementation based on TCP protocol.
 *
 * @see     wosh::NetworkManager
 ****************************************************************************
 * @version $Id: NetworkProtocolTcp.h 2856 2010-08-06 21:09:29Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Network_NetworkProtocolTcp_H__
 #define __WOSH_Network_NetworkProtocolTcp_H__

 #include <core/NetworkProtocol.h>
// #include <core/NetworkManager.h>
 #include <framework/network/NetworkChannelTcp.h>
 #include <framework/network/NetworkChannelTcpServer.h>


namespace wosh {
 namespace network {

 #define _NetworkProtocolTcp_NAME			"NetworkProtocolTCP"
 #define _NetworkProtocolTcp_PREFIX			"TCP://"
 #define _NetworkProtocolTcp_Priority		60


class NetworkProtocolTcp :	public wosh::NetworkProtocol,
							public virtual wosh::network::INetworkChannelTcpServerListener
 {
	WOSH_CLASSNAME(wosh::network::NetworkProtocolTcp)
	WOSH_CLASS_ABSTRACT(false)


	typedef ListT<wosh::network::NetworkChannelTcpServer*> tChannelTcpList;

	public:
		NetworkProtocolTcp();
		virtual ~NetworkProtocolTcp();


	public:
		inline const char* getProtocolPrefix() const	{ return _NetworkProtocolTcp_PREFIX; }

		inline int getPriority() const					{ return _NetworkProtocolTcp_Priority; }

		inline int getServersCount() const				{ return this->Servers.size(); }
		inline int getClientsCount() const				{ return -1; }

		bool supportProtocol( const std::string& protocol_string ) const;

	public:
		WRESULT listenChannel( const std::string& protocol_string, unsigned long timeout = LIMIT_ULONG_MAX );

		WRESULT connectChannel( const std::string& protocol_string, unsigned long timeout = LIMIT_ULONG_MAX );
		WRESULT connectChannel( const std::vector<std::string>& protocolsAvailable, unsigned long timeout = LIMIT_ULONG_MAX );

		WRESULT stopListeningChannels( unsigned long timeout = LIMIT_ULONG_MAX );

	public:
		void channelTcpServer_incomingConnection( wosh::network::NetworkChannelTcpServer* source, int socket_id );

	protected:
		tChannelTcpList Servers;

	private:
		NetworkProtocolTcp(const NetworkProtocolTcp&) { }
		NetworkProtocolTcp& operator=(const NetworkProtocolTcp&) { return *this; }


}; // class def

 }; // namespace network
}; // namespace wosh

#endif //__WOSH_Network_NetworkProtocolTcp_H__
